//
//  SettingVC.m
//  LazyHome
//
//  Created by yh on 2018/5/7.
//  Copyright © 2018年 yh. All rights reserved.
//

#import "SettingVC.h"
#import "MyDevicesVC.h"
#import "ServerSettingVC.h"

typedef enum : NSUInteger {
    SettingMenuTypeMyDevice,
    SettingMenuTypeServerSet
} SettingMenuType;


@interface SettingMenu : NSObject
@property (nonatomic ,copy) NSString *title;
@property (nonatomic, assign) SettingMenuType menuType;
@end

@implementation SettingMenu

+ (instancetype)menuWithTitle:(NSString *)title menuType:(SettingMenuType)menuType{
    SettingMenu *menu = [[SettingMenu alloc] init];
    menu.title = title;
    menu.menuType = menuType;
    return menu;
}

@end


@interface SettingVC ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) NSMutableArray *menus;
@end

@implementation SettingVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupTableView];
    [self menuInit];
}

- (void)setupTableView{
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"defultCell"];
}

- (void)menuInit{
    NSMutableArray *menusOne = [[NSMutableArray alloc]init];
    [menusOne addObject:[SettingMenu menuWithTitle:@"我的设备" menuType:SettingMenuTypeMyDevice]];
    [menusOne addObject:[SettingMenu menuWithTitle:@"服务器配置" menuType:SettingMenuTypeServerSet]];
    
    [self.menus addObject:menusOne];
    [self.tableView reloadData];
}


#pragma -mark UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.menus[section] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SettingMenu *menuItem = self.menus[indexPath.section][indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"defultCell" forIndexPath:indexPath];
    cell.textLabel.text = menuItem.title;
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    SettingMenu *menuItem = self.menus[indexPath.section][indexPath.row];
    if (menuItem.menuType == SettingMenuTypeMyDevice) {
        MyDevicesVC *devices = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"MyDevicesVC"];
        devices.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:devices animated:YES];
    }
    
    else if (menuItem.menuType == SettingMenuTypeServerSet) {
        ServerSettingVC *settingVC = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"ServerSettingVC"];
        settingVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:settingVC animated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (NSMutableArray *)menus{
    if(!_menus){
        _menus = [[NSMutableArray alloc] init];
    }
    return _menus;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
